/**
 * @description 关于css class的方法总结
 * @author zengwe
 * @date 2019/09/10
 */
export class Tools {
    /**
     * 
     * @param objs {[key: string]: any}[]
     * @description 将多个key:value的对象合并成一个，后面的覆盖前面，只有一层
     * @author zengwe
     */
    static combineObj(...objs:{[key: string]: any}[]) {
        let res: {[key: string]: any} = {};
        for(let obj of objs) {
            for(let key in obj) {
                res[key] = obj[key];
            }
        }
        return res;
    }
    /**
     * 
     * @param styleStr String
     * @description 将css的字符串转换未object
     * @author zengwe
     */
    static styleToObj(styleStr: string): {[key: string]: any} {
        if(Object.prototype.toString.call(styleStr) != '[object String]') {
            return {};
        }
        let temp = styleStr.split(';');
        let style: {[key:string]: any} = {};
        for(let item of temp) {
            if(item == '') {
                continue;
            }
            let itemArr = item.split(':');
            style[itemArr[0]] = itemArr[1];
        }
        return style;
    }
    /**
     * @description 将object转换为css style字符串
     * @param obj {[key: string]: any}
     * @author zengwe
     */
    static objToStyle(obj: {[key: string]: any}) {
        let style = '';
        for(let key in obj) {
            style += `${key}:${obj[key]};`;
        }
        return style;
    }
    /**
     * @description 创建dom
     * @param tag string 标签名称
     * @param style object 样式
     * @param attr obj 属性
     * @author zengwe
     */
    static createDom<T = HTMLElement>(tag: string, style: {[key: string]: any} = {}, attr: {[key: string]: any} = {}): T {
        let ele = document.createElement(tag) as HTMLElement;
        let styleStr = '';
        for(let key in style) {
            styleStr += `${key}: ${style[key]};`;
        }
        if(styleStr != '') {
            ele.setAttribute('style', styleStr);
        }
        for(let key in attr) {
            if(attr[key] == '') continue;
            ele.setAttribute(key, attr[key]);
        }
        return <any>ele;
    }
    /**
     * @description 将attrs中的key的px去掉，并转成number
     * @param style object css样式类
     * @param attrs 需要转换的key
     * @author zengwe
     */
    static styleAttrPxToNum<T = {[key: string]: any}>(style: {[key: string]: any}, attrs: string[]): T {
        let newStyle: any = {};
        for(let key in style) {
            if(attrs.indexOf(key) != -1) {
                newStyle[key] = Number(style[key].replace('px', ''));
            }else{
                newStyle[key]= style[key];
            }
        }
        return <T>newStyle;
    }
    /**
     * @description 将attrs中的key的px加上，并转成String
     * @param style object css样式类
     * @param attr 需要转换的key
     * @author zengwe
     */
    static styleAttrNumToPx<T = {[key: string]: any}>(style: {[key: string]: any}, attr: string[]): T  {
        let newStyle: any = {};
        for(let key in style) {
            if(attr.indexOf(key) != -1) {
                newStyle[key] = String(style[key]) + 'px';
            }else{
                newStyle[key]= style[key];
            }
        }
        return <T>newStyle;
    }
    static assign(obj: { [key: string]: any}, obj2: { [key: string]: any}) {
        for(let key in obj2) {
            if(obj[key] == undefined) {
                obj[key] = obj2[key];
                continue;
            }
            if(Object.prototype.toString.call(obj2[key]) == '[object Object]') {
                Tools.assign(obj[key], obj2[key]);
            }else{
                obj[key] = obj2[key];
            }
        }
        return obj;
    }
}